//
//  YJNetWorkManger.m
//  FashionShow
//
//  Created by 张龙 on 2017/2/27.
//
//

#import "YJNetWorkManger.h"

@implementation YJNetWorkManger
+ (instancetype)shareManager{
    static YJNetWorkManger *network;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        network=[[YJNetWorkManger alloc]init];
//        network.requestSerializer = [AFJSONRequestSerializer serializer];
        network.responseSerializer = [AFHTTPResponseSerializer serializer];
//        [network.requestSerializer setHTTPShouldHandleCookies:NO];
//        [network.requestSerializer setValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",[YJNetWorkManger uuidString]] forHTTPHeaderField:@"Content-Type"];
    });
    return network;
}
+ (NSString *)uuidString
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    CFRelease(uuid_ref);
    CFRelease(uuid_string_ref);
    return [uuid lowercaseString];
}
- (void)requestType:(RequestType)type andURL:(NSString *)urlstr andParmas:(id)parmas andComplition:(void (^)(id response,BOOL isuccess))complition{
    MBProgressHUD *hud=[MBProgressHUD showProgress:@"loading..."];
    void (^success)(NSURLSessionDataTask *task, id _Nullable responseObject)=^(NSURLSessionDataTask *task, id _Nullable responseObject){
        [hud hideAnimated:YES];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];

        NSString *str = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([str isEqualToString:@"0"]) {
            NSDictionary *resultDic = dic[@"data"];
            resultDic = resultDic?resultDic:@{};
            if (complition) {
                complition(resultDic,YES);
            }
        }
        else
            [MBProgressHUD showMessage:dic[@"msg"]];

    };
    void (^failure)(NSURLSessionDataTask * _Nullable task, NSError *error)=^(NSURLSessionDataTask * _Nullable task, NSError *error){
        [hud hideAnimated:YES];
        MyLog(@"-------------------------%@",error);
        complition(nil,false);
    };

           //拷贝回去
    if (type==GET) {
        [self GET:urlstr parameters:parmas progress:nil success:success failure:failure];
    }
    else
        [self POST:urlstr parameters:parmas progress:nil success:success failure:failure];

}

@end
